package org.cskt.sl.member.service.impl;

import java.util.List;

import org.cskt.sl.member.dao.UsersMapper;
import org.cskt.sl.member.entity.Users;
import org.cskt.sl.member.entity.UsersExample;
import org.cskt.sl.member.service.UsersService;
import org.cskt.sl.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 会员信息 service 实现类
 * 
 * @author guddqs
 *
 */
@Service
public class UsersServiceImpl implements UsersService {

	@Autowired
	private UsersMapper mapper;

	@Override
	public int countByExample(UsersExample example) {
		return mapper.countByExample(example);
	}

	@Override
	public int deleteById(Integer uid) {
		return mapper.deleteByPrimaryKey(uid);
	}

	@Override
	public int addUser(Users record) {
		return mapper.insertSelective(record);
	}

	@Override
	public List<Users> findUserPageList(UsersExample example, Pager pager) {
		if (pager != null) {
			if (pager.getCurrentPage() != null) {
				int first = (pager.getCurrentPage() - 1) * pager.getPageSize();
				return mapper.selectByExample(example, first, pager.getPageSize());
			}
		}
		return null;
	}

	@Override
	public Users getUserById(Integer uid) {
		return mapper.selectByPrimaryKey(uid);
	}

	@Override
	public int updateUserById(Users record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updatePwdById(int id, String pwd) {
		Users record = new Users();
		record.setUid(id);
		record.setPassword(pwd);
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateSecondPwdById(int id, String secondpwd) {
		Users record = new Users();
		record.setUid(id);
		record.setHighpass(secondpwd);
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateBalanceById(Integer id, Float balance) {
		Users record = new Users();
		record.setUid(id);
		record.setBalance(balance);
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateJifenById(Integer id, Integer jifen) {
		Users record = new Users();
		record.setUid(id);
		record.setConsumebalance(jifen);
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateMemberTypeById(Integer id, Integer mtid) {
		Users record = new Users();
		record.setUid(id);
		record.setMembertype(mtid);
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public Users login(Users user) {
		UsersExample example=new UsersExample();
		example.createCriteria().andUseridEqualTo(user.getUserid()).andPasswordEqualTo(user.getPassword());
		List<Users> list=mapper.selectByExample(example, 0, 1);
		if(list!=null&&list.size()==1){
			return list.get(0);
		}
		return null;
	}

}
